package cn.edu.baiyunu.myapplication.ui.create

import android.content.Context
import android.graphics.Bitmap
import android.net.Uri
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import cn.edu.baiyunu.myapplication.utils.ImageUtils
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import java.io.File

/**
 * 作品创作页ViewModel
 */
class CreateViewModel : ViewModel() {

    // 页面状态
    sealed class CreateScreenState {
        object Initial : CreateScreenState()
        object PhotoSelection : CreateScreenState()
        object CameraPreview : CreateScreenState()
        object PhotoPreview : CreateScreenState()
        object Processing : CreateScreenState()
        object Success : CreateScreenState()
        data class Error(val message: String) : CreateScreenState()
    }

    // 状态流
    private val _screenState = MutableStateFlow<CreateScreenState>(CreateScreenState.Initial)
    val screenState: StateFlow<CreateScreenState> = _screenState.asStateFlow()

    // 选中的图片URI
    private val _selectedImageUri = MutableStateFlow<Uri?>(null)
    val selectedImageUri: StateFlow<Uri?> = _selectedImageUri.asStateFlow()

    // 拍摄或选择的图片
    private val _capturedBitmap = MutableStateFlow<Bitmap?>(null)
    val capturedBitmap: StateFlow<Bitmap?> = _capturedBitmap.asStateFlow()

    // 提示消息
    private val _snackbarMessage = MutableStateFlow<String?>(null)
    val snackbarMessage: StateFlow<String?> = _snackbarMessage.asStateFlow()

    // 是否正在处理
    var isProcessing by mutableStateOf(false)
        private set

    // 是否显示拖放提示
    var isDragging by mutableStateOf(false)
        private set

    // 当前创作标题
    var createTitle by mutableStateOf("")
        private set

    // 当前创作描述
    var createDescription by mutableStateOf("")
        private set

    /**
     * 设置页面状态
     */
    fun setScreenState(state: CreateScreenState) {
        _screenState.value = state
    }

    /**
     * 设置选中的图片URI
     */
    fun setSelectedImageUri(uri: Uri?) {
        _selectedImageUri.value = uri
        if (uri != null) {
            setScreenState(CreateScreenState.PhotoPreview)
        }
    }

    /**
     * 设置拍摄的图片
     */
    fun setCapturedBitmap(bitmap: Bitmap?) {
        _capturedBitmap.value = bitmap
        if (bitmap != null) {
            setScreenState(CreateScreenState.PhotoPreview)
        }
    }

    /**
     * 显示提示消息
     */
    fun showSnackbar(message: String) {
        _snackbarMessage.value = message
        viewModelScope.launch {
            delay(3000)
            _snackbarMessage.value = null
        }
    }

    /**
     * 清除图片
     */
    fun clearImage() {
        _capturedBitmap.value = null
        _selectedImageUri.value = null
        setScreenState(CreateScreenState.PhotoSelection)
    }

    /**
     * 设置拖放状态
     */
    fun updateDraggingState(dragging: Boolean) {
        isDragging = dragging
    }

    /**
     * 保存图片到文件系统
     */
    fun saveBitmapToFile(context: Context, bitmap: Bitmap): File? {
        return ImageUtils.saveBitmapToFile(context, bitmap)
    }

    /**
     * 从URI加载位图
     */
    fun loadBitmapFromUri(context: Context, uri: Uri): Bitmap? {
        return ImageUtils.loadBitmapFromUri(context, uri)
    }

    /**
     * 处理作品
     */
    fun processArtwork(onComplete: () -> Unit) {
        if (_capturedBitmap.value == null && _selectedImageUri.value == null) {
            showSnackbar("请选择或拍摄一张照片")
            return
        }

        isProcessing = true
        _screenState.value = CreateScreenState.Processing

        viewModelScope.launch {
            // 延长处理时间，确保用户能看到处理状态
            delay(2500)
            isProcessing = false
            _screenState.value = CreateScreenState.Success
            onComplete()
        }
    }

    /**
     * 取消创作
     */
    fun cancelCreation() {
        clearImage()
        setScreenState(CreateScreenState.Initial)
    }

    /**
     * 更新创作信息
     */
    fun updateCreationInfo(title: String, description: String) {
        createTitle = title
        createDescription = description
    }
} 